<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>DeviceOrientationControls控制器案例</title>
    <style type="text/css">
        html, body {
            margin: 0;
            height: 100%;
        }

        canvas {
            display: block;
        }

    </style>
</head>
<body onload="draw();">

</body>
<script src="../js/three.js"></script>
<script src="../js/controls/DeviceOrientationControls.js"></script>
<script src="../js/stats.min.js"></script>
<script src="../js/dat.gui.min.js"></script>
<script>
    var renderer, camera, scene, gui, stats, ambientLight, directionalLight, control;

    function initRender() {
        renderer = new THREE.WebGLRenderer({antialias: true});
        renderer.setSize(window.innerWidth, window.innerHeight);
        //告诉渲染器需要阴影效果
        renderer.shadowMap.enabled = true;
        renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是，没有设置的这个清晰 THREE.PCFShadowMap
        document.body.appendChild(renderer.domElement);
    }

    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(0, 50, 0);
        camera.lookAt(new THREE.Vector3(0, 0, 0));
    }

    function initScene() {
        scene = new THREE.Scene();
    }

    function initGui() {
        //声明一个保存需求修改的相关数据的对象
        gui = {
            enabled:true
        };

        var datGui = new dat.GUI();
        //将设置属性添加到gui当中，gui.add(对象，属性，最小值，最大值）
        datGui.add(gui, "enabled").name("开启控制器").onChange(function (e) {
            control.enabled = e;
        })
    }

    function initLight() {
        ambientLight = new THREE.AmbientLight("#111111");
        scene.add(ambientLight);

        directionalLight = new THREE.DirectionalLight("#ffffff");
        directionalLight.position.set(40, 60, 10);

        directionalLight.shadow.camera.near = 1; //产生阴影的最近距离
        directionalLight.shadow.camera.far = 400; //产生阴影的最远距离
        directionalLight.shadow.camera.left = -50; //产生阴影距离位置的最左边位置
        directionalLight.shadow.camera.right = 50; //最右边
        directionalLight.shadow.camera.top = 50; //最上边
        directionalLight.shadow.camera.bottom = -50; //最下面

        //这两个值决定生成阴影密度 默认512
        directionalLight.shadow.mapSize.height = 1024;
        directionalLight.shadow.mapSize.width = 1024;

        //告诉平行光需要开启阴影投射
        directionalLight.castShadow = true;

        scene.add(directionalLight);
    }

    function initModel() {

        //创建一个最普通的精灵
        var spriteMaterialNormal = new THREE.SpriteMaterial({color: 0x00ffff});
        var spriteNormal = new THREE.Sprite(spriteMaterialNormal);
        spriteNormal.position.set(-30, 10, 0); //设置位置
        spriteNormal.scale.set(5, 5, 1); //设置scale进行大小设置
        scene.add(spriteNormal);

        //球体
        var sphereGeometry = new THREE.SphereGeometry(5, 24, 16);
        var sphereMaterial = new THREE.MeshStandardMaterial({color: 0xff00ff});
        var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
        sphere.castShadow = true; //开启阴影
        directionalLight.target = sphere; //平行光的焦点到球
        scene.add(sphere);

        //添加一个精灵 使用了将canvas生成img的src导入的方式
        var spriteMap = new THREE.TextureLoader().load(drawCanvas({text:"球", width:64, height:64}).toDataURL());
        var spriteMaterial = new THREE.SpriteMaterial({map: spriteMap, color: 0xffffff});
        var sprite = new THREE.Sprite(spriteMaterial);
        sprite.position.set(0, 10, 0); //设置位置
        sprite.scale.set(5, 5, 1); //设置scale进行大小设置
        scene.add(sprite);

        //立方体
        var cubeGeometry = new THREE.CubeGeometry(10, 10, 10);
        var cubeMaterial = new THREE.MeshPhongMaterial({color: 0x00ffff});
        var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
        cube.position.x = 30;
        cube.position.z = -5;
        cube.castShadow = true; //开启阴影
        scene.add(cube);

        //添加一个精灵
        var canvas = drawCanvas({text:"立方体", width:256, height:64});
        var spriteMapCube = new THREE.Texture(canvas);
        spriteMapCube.wrapS = THREE.RepeatWrapping;
        spriteMapCube.wrapT = THREE.RepeatWrapping;
        spriteMapCube.needsUpdate = true;

        var spriteCube = new THREE.Sprite(new THREE.SpriteMaterial({map: spriteMapCube, color: 0xffffff}));
        spriteCube.position.set(30, 10, -5); //设置位置
        spriteCube.scale.set(20, 5, 1); //设置scale进行大小设置
        spriteCube.center.set(0.5, 0); //设置位置点处于精灵的最下方中间位置
        scene.add(spriteCube);

        //底部平面
        var planeGeometry = new THREE.PlaneGeometry(100, 100);
        var planeMaterial = new THREE.MeshLambertMaterial({color: 0xaaaaaa, side: THREE.DoubleSide});
        var plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.rotation.x = -0.5 * Math.PI;
        plane.position.y = -5.1;
        plane.receiveShadow = true; //可以接收阴影
        scene.add(plane);

        //创建一个星空的效果
        var starsGeometry = new THREE.Geometry();
        //生成一万个点的位置
        for (var i = 0; i < 10000; i++) {

            var star = new THREE.Vector3();
            //THREE.Math.randFloatSpread 在区间内随机浮动* - 范围 / 2 *到* 范围 / 2 *内随机取值。
            star.x = THREE.Math.randFloatSpread(2000);
            star.y = THREE.Math.randFloatSpread(2000);
            star.z = THREE.Math.randFloatSpread(2000);

            starsGeometry.vertices.push(star);

        }
        var starsMaterial = new THREE.PointsMaterial({color: 0x888888});
        var starField = new THREE.Points(starsGeometry, starsMaterial);
        scene.add(starField);

    }

    //创建canvas对象
    function drawCanvas(options) {
        let canvas = document.createElement("canvas");
        canvas.width = options.width;
        canvas.height = options.height;

        let ctx = canvas.getContext("2d");
        ctx.fillStyle = "rgba(0, 0, 0, 0)";
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.font = "60px Verdana";
        ctx.fillStyle = "#fff";
        ctx.fillText(options.text, 0, 56, options.width);
        return canvas;
    }

    function initStats() {
        stats = new Stats();
        document.body.appendChild(stats.dom);
    }

    function initControl() {

        control = new THREE.DeviceOrientationControls(camera);

        // 是否开启当前的控制器 默认值为true
        control.enabled = true;
    }

    function render() {

        control.update();

        renderer.render(scene, camera);
    }

    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);

    }

    function animate() {
        //更新控制器
        render();

        //更新性能插件
        stats.update();

        requestAnimationFrame(animate);
    }

    function draw() {
        initGui();
        initRender();
        initScene();
        initCamera();
        initLight();
        initModel();
        initStats();

        initControl();

        animate();
        window.onresize = onWindowResize;
    }
</script>
</html>